package com.bilibili.juc.volatiles;

import java.util.concurrent.TimeUnit;

public class VolatileSeeDemo01 {

    static boolean flag = true;//不加volatile,没有可见性
    //static volatile boolean flag = true; //加volatile,保证可见性

    public static void main(String[] args) {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " come in");
            while (flag) {
               //System.out.println("--------------");
                new Integer(100);
            }
            System.out.println("t1 over");
        }, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            flag = false;
        }, "t2").start();
    }
}


  //      原文链接：https://blog.csdn.net/weixin_43899792/article/details/124492448
